import { get, post, put } from '@/utils/request';

// 定义接口类型
export interface AlertInfo {
  id: number;
  title: string;
  content: string;
  source: string;
  sourceId: string;
  severity: 'P1' | 'P2' | 'P3';
  status: 'TRIGGERED' | 'ACKED' | 'RESOLVED';
  assignedTo: {
    id: number;
    username: string;
    realName: string;
    avatar: string;
  } | null;
  createTime: string;
  updateTime: string;
  timeline: Array<{
    id: number;
    alertId: number;
    action: string;
    operator: {
      id: number;
      username: string;
      realName: string;
      avatar: string;
    };
    comment: string;
    createTime: string;
  }>;
}

export interface AlertSourceInfo {
  id: number;
  name: string;
  type: 'PROMETHEUS' | 'ZABBIX' | 'GRAFANA' | 'CUSTOM_WEBHOOK';
  config: Record<string, any>;
  status: 'ACTIVE' | 'INACTIVE';
  createTime: string;
  updateTime: string;
}

// 获取告警列表
export function getAlertList(params: any) {
  return get<{
    total: number;
    list: AlertInfo[];
  }>('/alert/list', params);
}

// 获取告警详情
export function getAlertDetail(id: number) {
  return get<AlertInfo>(`/alert/${id}`);
}

// 认领告警
export function claimAlert(id: number) {
  return post(`/alert/${id}/claim`);
}

// 转派告警
export function transferAlert(id: number, userId: number, comment?: string) {
  return post(`/alert/${id}/transfer`, { userId, comment });
}

// 关闭告警
export function resolveAlert(id: number, comment?: string) {
  return post(`/alert/${id}/resolve`, { comment });
}

// 添加告警评论
export function addAlertComment(id: number, comment: string) {
  return post(`/alert/${id}/comment`, { comment });
}

// 获取告警源列表
export function getAlertSourceList() {
  return get<AlertSourceInfo[]>('/alert/sources');
}

// 创建告警源
export function createAlertSource(data: Omit<AlertSourceInfo, 'id' | 'createTime' | 'updateTime'>) {
  return post('/alert/source', data);
}

// 更新告警源
export function updateAlertSource(id: number, data: Partial<AlertSourceInfo>) {
  return put(`/alert/source/${id}`, data);
}

// 获取告警统计数据
export function getAlertStats(params: { startDate: string; endDate: string; teamId?: number }) {
  return get('/alert/stats', params);
} 